package com.shycloud.mido.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.security.util.SecurityUtils;
import com.shycloud.mido.music.domain.MisMusicCategory;
import com.shycloud.mido.music.mapper.MisMusicCategoryMapper;
import com.shycloud.mido.music.vo.MisMusicVO;
import com.shycloud.mido.music.vo.MyCollectionVO;
import com.shycloud.mido.music.vo.RecommendCollectionVO;
import com.shycloud.mido.music.vo.RecommendConfirmEditVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.music.domain.MisMusicRecommend;
import com.shycloud.mido.music.mapper.MisMusicRecommendMapper;
import com.shycloud.mido.music.service.MisMusicRecommendService;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
public class MisMusicRecommendServiceImpl extends ServiceImpl<MisMusicRecommendMapper, MisMusicRecommend> implements
		MisMusicRecommendService {

	/** 推荐曲集 mapper */
	private MisMusicRecommendMapper recommendMapper;

	/** 分类 mapper */
	private MisMusicCategoryMapper categoryMapper;

	/**
	 * 获取推荐曲集
	 *
	 * @return java.util.List<com.shycloud.mido.music.vo.MyCollectionVO>
	 * @author nianhua.jiang
	 * @date 2020/9/1 9:32
	 **/
	@Override
	public List<MyCollectionVO> getRecommendCollection(Integer cateId) {
		return recommendMapper.getRecommendCollection(cateId);
	}

	@Override
	public Integer getRecommendCollectionCount(Integer cateId) {
		return recommendMapper.getRecommendCollectionCount(cateId);
	}

	/**
	 * 获取推荐曲目
	 *
	 * @return java.util.List<com.shycloud.mido.music.vo.MisMusicVO>
	 * @author nianhua.jiang
	 * @date 2020/9/1 10:11
	 **/
	@Override
	public List<MisMusicVO> getRecommendMusic(Integer cateId) {

		if (SecurityUtils.getMember() == null) {
			return recommendMapper.getRecommendMusic("NB", cateId);
		} else {
			return recommendMapper.getRecommendMusic(SecurityUtils.getMember().getId(), cateId);
		}

	}

	@Override
	public Integer getRecommendMusicCount(Integer cateId) {
		if (SecurityUtils.getMember() == null) {
			return recommendMapper.getRecommendMusicCount("NB", cateId);
		} else {
			return recommendMapper.getRecommendMusicCount(SecurityUtils.getMember().getId(), cateId);
		}
	}

	@Override
	public List<RecommendCollectionVO> getRecommendCollectionAdmin(Integer cateId) {
		return recommendMapper.getRecommendCollectionAdmin(cateId);
	}

	@Override
	public List<MisMusicVO> getRecommendMusicAdmin(Integer cateId) {

		if (SecurityUtils.getMember() == null) {
			return recommendMapper.getRecommendMusicAdmin("NB", cateId);
		} else {
			return recommendMapper.getRecommendMusicAdmin(SecurityUtils.getMember().getId(), cateId);
		}

	}

	@Override
	public IPage<MyCollectionVO> getMoreThenCollection(Page page, Integer cateId) {
		return recommendMapper.getMoreThenCollection(page, cateId);
	}

	@Override
	public IPage<MisMusicVO> getMoreThenMusic(Page page, Integer cateId) {
		if (SecurityUtils.getMember() == null) {
			return recommendMapper.getMoreThenMusic(page, "没有登陆的场合", cateId);
		}
		return recommendMapper.getMoreThenMusic(page, SecurityUtils.getMember().getId(), cateId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean confirmEdit(RecommendConfirmEditVO confirmEditVO) throws Exception {
		Integer categoryId = confirmEditVO.getCategoryId();

		List<Map> musicIdList = confirmEditVO.getRecomMusicList();
		List<Map> collectionIdList = confirmEditVO.getRecomCollectionList();
		List<Integer> musicIdsList = new ArrayList<>();
		List<Integer> collectionIdsList = new ArrayList<>();

		for (Map map : musicIdList) {
			musicIdsList.add((Integer) map.get("musicId"));
		}
		for (Map map : collectionIdList) {
			collectionIdsList.add((Integer) map.get("collectionId"));
		}

		List<MisMusicRecommend> existingMusicIdList = recommendMapper.getRecommendMusicIds(categoryId);
		List<MisMusicRecommend> existingCollectionIdList = recommendMapper.getRecommendCollectionIds(categoryId);
		//处理推荐曲目更新
		outer:
		for (int i = 0; i < musicIdList.size(); i++) {
			Map musicMap = (Map) musicIdList.get(i);
			Integer musicId = (Integer) musicMap.get("musicId");
			Integer sort = (Integer) musicMap.get("sort");

			MisMusicRecommend entity = new MisMusicRecommend();
			entity.setMusicId(musicId);
			entity.setStatus("0");
			entity.setSort(sort);
			entity.setCategoryId(categoryId);

			for (MisMusicRecommend existing : existingMusicIdList) {
				if (existing.getMusicId().equals(musicId)) {
					if (existing.getSort().equals(sort)) {
						continue outer;
					} else {
						//sort有改变，说明应该进行更新操作
						entity.setId(existing.getId());
						recommendMapper.updateById(entity);
						continue outer;
					}
				}
			}

			recommendMapper.insert(entity);
		}
		//删除没传过来的曲目
		for (int i = 0; i < existingMusicIdList.size(); i++) {
			Integer musicId = existingMusicIdList.get(i).getMusicId();
			if (!musicIdsList.contains(musicId)) {
				Map map = new HashMap();
				map.put("music_id", musicId);
				map.put("category_id", categoryId);
				recommendMapper.deleteByMap(map);
			}
		}

		//处理推荐曲集更新
		outer:
		for (int i = 0; i < collectionIdList.size(); i++) {
			Map collectionMap = (Map) collectionIdList.get(i);
			Integer collectionId = (Integer) collectionMap.get("collectionId");
			Integer sort = (Integer) collectionMap.get("sort");

			MisMusicRecommend entity = new MisMusicRecommend();
			entity.setCollectionId(collectionId);
			entity.setStatus("0");
			entity.setSort(sort);
			entity.setCategoryId(categoryId);

			for (MisMusicRecommend existing : existingCollectionIdList) {
				if (existing.getCollectionId().equals(collectionId)) {
					if (existing.getSort().equals(sort)) {
						continue outer;
					} else {
						//sort有改变，说明应该进行更新操作
						entity.setId(existing.getId());
						recommendMapper.updateById(entity);
						continue outer;
					}
				}
			}

			recommendMapper.insert(entity);
		}
		//删除没传过来的曲集
		for (int i = 0; i < existingCollectionIdList.size(); i++) {
			Integer collectionId = existingCollectionIdList.get(i).getCollectionId();
			if (!collectionIdsList.contains(collectionId)) {
				Map map = new HashMap();
				map.put("collection_id", collectionId);
				map.put("category_id", categoryId);
				recommendMapper.deleteByMap(map);
			}
		}

		//更新类别的推荐曲目和曲集APP端显示标题
		MisMusicCategory categoryEntity =
				categoryMapper.selectOne(
						new QueryWrapper<MisMusicCategory>().eq("id", categoryId));
		if (categoryEntity == null) {
			throw new BusinessException("未找到此分类");
		}
		categoryEntity.setMusicTitle(confirmEditVO.getRecomMusicTitle());
		categoryEntity.setCollectionTitle(confirmEditVO.getRecomCollectionTitle());
		categoryMapper.updateById(categoryEntity);

		return true;
	}
}
